package rpg.data;

import java.io.File;

import org.jdom.Element;

import pgui.visual.PImage;
import square.data.DataGroup;
import square.data.SpriteData;

/**
 * 
 * @author Pedro Silva Moreira
 */
public class SpriteXML implements LoaderXML {
	private static SpriteXML singleton = new SpriteXML();

	// Creation
	private SpriteXML() {
	}

	// Instance
	public static SpriteXML instance() {
		return singleton;
	}

	// Load
	public void load(String group, String name, Element origin) {
		Element elem = origin;
		SpriteData data = new SpriteData();

		// Group
		data.setGroup(group);

		// Name
		data.setName(elem.getAttributeValue("name"));

		// Attributes
		String countString = elem.getAttributeValue("count");
		Integer count = countString != null ? Integer.valueOf(countString) : 1;

		String animationString = elem.getAttributeValue("animation");
		if (animationString != null)
			data.setAnimation(Integer.valueOf(animationString));

		for (int it = 0; it < count; it++) {
			PImage component = new PImage();
			component.loadImage("data" + File.separator + "sprites"
					+ File.separator + group + File.separator + data.getName()
					+ File.separator + it + ".png");
			data.getFrames().add(component);

			DataGroup.getSprites().add(data);
		}
	}
}